package bizapi.sinosoft.com.provider.order.util;


import cart.model.jdorder.*;
import cart.model.order.OrderMain;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xujingfeng on 2016年12月16日
 */
public class OrderUtil {

    public static JdOrder createJdOrderByCorder(Corder corder) {
        JdOrder jdOrder = new JdOrder();

        //设置子订单
        corder.setPorder(corder.getJdorderid());
        List<Corder> corderList = new ArrayList<Corder>();
        corderList.add(corder);
        jdOrder.setCorder(corderList);

        //封装porder
        Porder pOrder = new Porder();
        pOrder.setFreight(corder.getFreight());
        pOrder.setJdorderid(corder.getJdorderid());
        pOrder.setOrdernakedprice(corder.getOrdernakedprice());
        pOrder.setOrderprice(corder.getOrderprice());
        pOrder.setOrdertaxprice(corder.getOrdertaxprice());
        pOrder.setSku(SkuUtil.createPSkuByCSku(corder.getSku()));
        //设置父订单
        jdOrder.setPorder(pOrder);

        //设置jdorder的其他属性
        jdOrder.setOrderstate(corder.getOrderstate());
        jdOrder.setSubmitstate(corder.getSubmitstate());
        jdOrder.setType(1);

        return jdOrder;
    }

    public static JdOrder formatOrder(JdOrder jdOrder) {
        jdOrder.getPorder().setJdOrder(jdOrder);
        for (PSku pSku : jdOrder.getPorder().getSku()) {
            pSku.setOrder(jdOrder.getPorder());
        }
        for (Corder corder : jdOrder.getCorder()) {
            corder.setJdOrder(jdOrder);
            for (CSku cSku : corder.getSku()) {
                cSku.setCorder(corder);
            }
        }
        return jdOrder;
    }

    /**
     * 有一个子订单新建，则整体是新建，全部妥投则算作妥投，否则是取消
     *
     * @param jdOrder
     * @return
     */
    public static String judgeState(JdOrder jdOrder) {
        List<Corder> corders = jdOrder.getCorder();
        List<Integer> states = new ArrayList<Integer>();
        for (Corder corder : corders) {
            states.add(corder.getState());
        }
        //默认拒收
        String confirmState = OrderMain.order_status_refuse;
        for (Integer state : states) {
            //新建=未发货
            if (state == 0) {
                confirmState = OrderMain.order_status_ounfilled;
                break;
            }
            //妥投=待评价
            if (state == 1) confirmState = OrderMain.order_status_evaluate;
        }
        return confirmState;
    }
}










